#include <stdio.h>
#include <time.h>

#define SIZE 100000

int binsearch(int x, int v[], int n);
int binsearch2(int x, int v[], int n);

int num[SIZE] = {};

int main()
{
    int i, n1, n2;
    clock_t start, end;
    for(i = 0; i < SIZE; i++){
        num[i] = i;
    }
        
    start = clock();
    for ( i = 0; i < 10000000; i++ ) {
        n1 = binsearch(7125, num, SIZE);
    }         
    end = clock();
    printf("n1 = %d, time = %lu clocks\n", n1, (end - start));

    start = clock();
    for ( i = 0; i < 10000000; i++ ) {
        n2 = binsearch2(7125, num, SIZE);
    }         
    end = clock();
    printf("n2 = %d, time = %lu clocks\n", n2, (end - start));

    return 0;
}

int binsearch(int x, int v[], int n)
{
    int low, high, mid;

    low = 0;
    high = n - 1;
    while (low <= high)
    {
        mid = (low + high) / 2;
        if(x < v[mid])
            high = mid - 1;
        else if(x > v[mid])
            low = mid + 1;
        else
            return mid;        
    }
    return -1;    
}

int binsearch2(int x, int v[], int n)
{
    int low, high, mid;

    low = 0;
    high = n - 1;
    mid = (low + high) / 2;
    while (low <= high && x != v[mid])
    {
        mid = (low + high) / 2;
        if(x < v[mid])
            high = mid - 1;
        else 
            low = mid;      
    }

    if(x == v[mid])
        return mid;
    
    return -1;    
}